import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class AveragePriceCalculator {
    public static void main(String[] args) {
        String fileName = "c:\\Users\\30762\\Desktop\\商品列表.txt"; 
        List<Double> prices = new ArrayList<>();

        
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = br.readLine()) != null) {
                try {
                    double price = Double.parseDouble(line.trim());
                    prices.add(price);
                } catch (NumberFormatException e) {
                    
                    System.out.println("无法解析的价格行: " + line);
                }
            }
        } catch (IOException e) {
            System.out.println("读取文件时发生错误: " + e.getMessage());
            return;
        }

       
        double averagePrice = prices.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);

        
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true))) {
            bw.write("\n平均价格: " + averagePrice);
        } catch (IOException e) {
            System.out.println("写入文件时发生错误: " + e.getMessage());
        }

        System.out.println("平均价格已计算并追加到文件中: " + averagePrice);
    }
}